package com.dgut.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.admin.service.WxUserService;
import com.dgut.common.entity.UserInfo;
import com.dgut.common.entity.WxUser;
import com.dgut.common.enums.StatusEnum;
import com.dgut.common.mapper.UserInfoMapper;
import com.dgut.common.mapper.WxUserMapper;
import com.dgut.common.utils.ApiException;
import com.dgut.common.utils.PageVO;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author it00zyq
 * @date 2022/1/8
 */
@Service
public class WxUserServiceImpl implements WxUserService {

    private final WxUserMapper wxUserMapper;
    private final UserInfoMapper infoMapper;

    public WxUserServiceImpl(WxUserMapper wxUserMapper, UserInfoMapper infoMapper) {
        this.wxUserMapper = wxUserMapper;
        this.infoMapper = infoMapper;
    }


    @Override
    public PageVO listWxUser(Integer pageNum, Integer pageSize, String name, Integer identity) {
        IPage<WxUser> page = new Page<WxUser>().setCurrent(pageNum).setSize(pageSize);
        IPage<WxUser> wxUserIPage = wxUserMapper.selectPageByCondition(page, name, identity);
        return PageVO.convert(wxUserIPage);
    }

    @Override
    public UserInfo detailWxUser(Integer id) {
        return infoMapper.selectByUserId(id, StatusEnum.SUCCESS_AUDIT.getState());
    }

    @Override
    public void disableUser(Integer id) {
        WxUser wxUser = wxUserMapper.selectById(id);
        if (wxUser == null) {
            throw new ApiException("【WxUserServiceImpl - disableUser】WxUser fail to find.");
        }
        if (Integer.valueOf(1).equals(wxUser.getDisabled())) {
            wxUser.setDisabled(0);
        } else {
            wxUser.setDisabled(1);
        }
        int result = wxUserMapper.updateById(wxUser);
        if (result != 1) {
            throw new ApiException("【WxUserServiceImpl - disableUser】fail to diable.");
        }
    }
}
